package com.servicesImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.dao.impl.DaoImpl;
import com.model.Student;
import com.services.StudentServices;

public class StudentServicesImpl implements StudentServices{
	
	DaoImpl daoImpl = new DaoImpl();
	PreparedStatement preparedStatement = null;
	Statement statement = null;
	ResultSet resultSet= null;
	Student student = null;
	List<Student> students = null;
	int i = 0;

	@Override
	public int createStudent(Student student) throws Exception {
		
	preparedStatement =	daoImpl.openconnection().prepareStatement("INSERT INTO STUDENT VALUES(?,?,?)");
	preparedStatement.setInt(1, student.getSid());
	preparedStatement.setString(2, student.getName());
	preparedStatement.setDouble(3, student.getFees());
	i = preparedStatement.executeUpdate();
	daoImpl.closeconnection();
	return i;
		
	}

	@Override
	public List<Student> getAll() throws Exception {
		students = new ArrayList<Student>();
		statement = daoImpl.openconnection().createStatement();
		resultSet = statement.executeQuery("SELECT * FROM STUDENT");
		
		while(resultSet.next()){
			
			student = new Student();
			student.setSid(resultSet.getInt(1));
			student.setName(resultSet.getString(2));
			student.setFees(resultSet.getDouble(3));
			students.add(student);
		}
		return students;
	}

	@Override
	public int updateStudent(Student student) throws  Exception {
		
		preparedStatement =	daoImpl.openconnection().prepareStatement("UPDATE STUDENT SET FEES=? WHERE SID=? AND SNAME =?");
		System.out.println(student.getSid()+student.getName()+student.getFees());
		preparedStatement.setDouble(1, student.getFees());
		preparedStatement.setInt(2, student.getSid());
		preparedStatement.setString(3, student.getName());
		
		i = preparedStatement.executeUpdate();
		
		daoImpl.closeconnection();
		
		return i;
	}

	@Override
	public int deleteStudent(Student student) throws  Exception {
		preparedStatement =	daoImpl.openconnection().prepareStatement("Delete FROM STUDENT WHERE SID=?");
		preparedStatement.setInt(1, student.getSid());
		i = preparedStatement.executeUpdate();
		daoImpl.closeconnection();
		return i;
	}

}
